/**
 * @(#)DateUtil.java
 *       
 * project：uniorder-common
 * 
 * Copyright @2012 - 2014 CHINA EASTERN AIRLINES CO., LTO.  All rights reserved.
 * ADDRESS: No.628,Suining Road,Changning District,Shanghai,201106 P.R.CHINA
 */
package com.ceair.flight.refund.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
* <p>时间工具类</p>
*
* @author kevin lee
* @version v1.0.0  12:02:24 AM, Jun 1, 2012
* @since JDK1.5
*/
public class DateUtil {

   //日期格式
   public static String yyyyMMdd         = "yyyyMMdd";
   public static String yyyyMMddHHmm     = "yyyyMMddHHmm";
   public static String yyyyMMddHHmmss   = "yyyyMMddHHmmss";
   public static String yyyyMMddHHmmssS  = "yyyyMMddHHmmssS";

   //中划线日期格式
   public static String yyyy_MM_dd            = "yyyy-MM-dd";
   public static String yyyy_MM_dd_HH_mm      = "yyyy-MM-dd HH:mm";
   public static String yyyy_MM_dd_HH_mm_ss   = "yyyy-MM-dd HH:mm:ss";
   public static String yyyy_MM_dd_HH_mm_ss_S = "yyyy-MM-dd HH:mm:ss.S";

   //反斜杠日期格式
   public static String YYYYsMMsDD            = "yyyy/MM/dd";
   public static String MMsDDsYYYY            = "MM/dd/yyyy";
   public static String DDsMMsYYYY            = "dd/MM/yyyy";

   //时间按
   public static String MM_dd = "MM-dd";
   public static String HH_mm = "HH:mm";  //24小时制
   public static String hh_mm = "hh:mm";  //12小时制
   public static String HHmm  = "HHmm";
   public static String hhmm  = "hhmm";

   //英文日期
   public static String ddMMM     = "ddMMM";       //09Oct
   public static String ddMMMyy   = "ddMMMyy";     //01Jan09
   public static String ddMMMyyyy = "ddMMMyyyy";   //01Jan2009

   public static Calendar getCalendar(Date date) {
       try{
           Calendar calendar = Calendar.getInstance();
           calendar.setFirstDayOfWeek(Calendar.MONDAY);
           calendar.setTime(date);
           return calendar;
       }catch (Exception e){ return null; }
   }

   /** 取得当前年、月、日、小时、分、秒等数值**/
   public static int getCurrentYear()   { return Calendar.getInstance().get(Calendar.YEAR);}
   public static int getCurrentMonth()  { return Calendar.getInstance().get(Calendar.MONTH);}
   public static int getCurrentDay()    { return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);}
   public static int getCurrentHour12() { return Calendar.getInstance().get(Calendar.HOUR);}
   public static int getCurrentHour24() { return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);}
   public static int getCurrentMinute() { return Calendar.getInstance().get(Calendar.MINUTE);}
   public static int getCurrentSecond() { return Calendar.getInstance().get(Calendar.SECOND);}

   /** 前**/
   public static String BEFORE = "-";

   /** 后**/
   public static String AFTER = "+";



   /**
    * 把日期类型按照格式转换成字符串，缺省的日期格式为yyyy-MM-dd
    * @param date        Date类型日期
    * @param dstFormater    日期格式，缺省为yyyy-MM-dd
    */
   public static String toString(Date date,String dstFormater){
       try {
       int month=date.getMonth();
       if(month<10) {
           String dateStr=new SimpleDateFormat(dstFormater).format(date);
           String[] s=dateStr.split("-");
           if(s[1].length()!=2) {
           s[1]="0"+s[1];
           }
           dateStr=s[0]+"-"+s[1]+"-"+s[2];
           return dateStr;
       }else {
           return new SimpleDateFormat(dstFormater).format(date);
       }
       } catch (Exception e) { return null; }
   }

    /**
     * 年龄计算
     * @param birthDay
     * @return
     * @throws Exception
     */
    public static  int getAge(Date birthDay) throws Exception {
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthDay)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;
            }else{
                age--;
            }
        }
        return age;
    }

    /**
     * 年龄计算
     * @param birthDay
     * @return
     * @throws Exception
     */
    public static  int getAge(String birthDay,String formatString) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat(formatString);
        Date date = format.parse(birthDay);
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthDay)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(date);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;
            }else{
                age--;
            }
        }
        return age;
    }

 public static void main(String[] args) {
     String dd="2017-8-9 14:29:20";

   Date date =new Date();
   Calendar calendar = Calendar.getInstance();
   calendar.setTime(date);
   calendar.add(calendar.DATE, -70);
   calendar.getTime();
   System.out.println(toString(calendar.getTime(),"yyyy-MM-dd HH:mm:ss"));

}


}
